package com.dmooo.yxmmerchant.view.ui;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.support.design.widget.TextInputEditText;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.common.util.CheckUtil;
import com.common.util.ToastUtil;
import com.dmooo.yxmmerchant.R;
import com.dmooo.yxmmerchant.base.BaseActivity;
import com.dmooo.yxmmerchant.view.contract.RegisterContract;
import com.dmooo.yxmmerchant.view.presenter.RegisterPrestener;

import butterknife.BindView;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity<RegisterPrestener> implements RegisterContract.RegisterView {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_one)
    TextInputEditText etOne;
    @BindView(R.id.et_two)
    TextInputEditText etTwo;
    @BindView(R.id.btn_code)
    TextView txtGetCode;
    @BindView(R.id.et_three)
    TextInputEditText etThree;
    @BindView(R.id.et_three_sure)
    TextInputEditText etThreeSure;
    @BindView(R.id.cb_agree)
    CheckBox cbAgree;
    private TimeCount time;
    @Override
    protected int getLayout() {

        return R.layout.activity_register;
    }

    @Override
    protected void initEventAndData() {
        tvTitle.setText("商户注册");
        time = new TimeCount(60000, 1000);
    }

    @Override
    protected void createPresenter() {
        mPresenter = new RegisterPrestener(this, this);
    }

    @Override
    public void getPhoneSuccess() {
        ToastUtil.getInstance().showSuccessMsg("短信发送成功，请注意查收");
        time.start();
    }

    @Override
    public void registerSuccess() {
        ToastUtil.getInstance().showSuccessMsg("注册成功");
        finish();
    }

    @Override
    public void submitProfileSuccess() {

    }

    @Override
    public void submitIdSuccess() {

    }

    @OnClick({R.id.tv_left, R.id.btn_code, R.id.tv_register, R.id.txt_agreement})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.btn_code:
                if(TextUtils.isEmpty(etOne.getText().toString())){
                    ToastUtil.getInstance().showErrorMsg("请输入手机号");
                    return;
                }
                if(!CheckUtil.isMobile(etOne.getText().toString().trim())){
                    ToastUtil.getInstance().showErrorMsg("请输入正确的手机号格式");
                    return;
                }
                mPresenter.getPhoneCode(etOne.getText().toString());
                break;
            case R.id.tv_register:
                if(TextUtils.isEmpty(etOne.getText().toString())){
                    ToastUtil.getInstance().showErrorMsg("请输入手机号");
                    return;
                }
                if(TextUtils.isEmpty(etTwo.getText().toString())){
                    ToastUtil.getInstance().showErrorMsg("请输入验证码");
                    return;
                }
                if(TextUtils.isEmpty(etThree.getText().toString())){
                    ToastUtil.getInstance().showErrorMsg("请设置登陆密码");
                    return;
                }
                if(TextUtils.isEmpty(etThreeSure.getText().toString())){
                    ToastUtil.getInstance().showErrorMsg("请确认登陆密码");
                    return;
                }
                if(!etThree.getText().toString().equals(etThreeSure.getText().toString())){
                    ToastUtil.getInstance().showErrorMsg("两次密码不相同");
                    return;
                }
                if(!cbAgree.isChecked()){
                    ToastUtil.getInstance().showErrorMsg("请阅读并勾选协议");
                    return;
                }
                mPresenter.doRegister(etOne.getText().toString().trim(),etTwo.getText().toString().trim(),etThree.getText().toString().trim());
                break;
            case R.id.txt_agreement:
//                Bundle bundle=new Bundle();
//                bundle.putString("article_id","1");
//                StartActivityUtil.startActivity(this,NewsDetailActivity.class,bundle);
                break;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        time.cancel();
    }

    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @SuppressLint("NewApi")
        @Override
        public void onTick(long millisUntilFinished) {// 计时过程显示
            txtGetCode.setClickable(false);
            txtGetCode.setText("倒计时" + millisUntilFinished / 1000 + "s");
            txtGetCode.setTextColor(Color.WHITE);
            txtGetCode.setBackground(getDrawable(R.drawable.shape_phone_code_unenable));
        }

        @SuppressLint("NewApi")
        @Override
        public void onFinish() {// 计时完毕时触发
            txtGetCode.setText("获取验证码");
            txtGetCode.setClickable(true);
            txtGetCode.setTextColor(getResources().getColor(R.color.white));
            txtGetCode.setBackground(getDrawable(R.drawable.shape_phone_code));
        }

    }
}
